package com.fengye.mpe.controller;

import cn.hutool.core.date.DateUtil;
import com.fengye.mpe.dto.ArticleDTO;
import com.fengye.mpe.dto.AuthorDTO;
import com.fengye.mpe.process.Article;
import com.fengye.mpe.process.Author;
import com.fengye.mpe.service.IArticleService;
import com.fengye.mpe.service.IAuthorService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author fengyexjtu@126.com
 * @since 2024-03-13
 */
@RestController
@RequestMapping("/author")
@RequiredArgsConstructor
@Slf4j
public class AuthorController {
    
    private final IAuthorService authorService;
    
    /**
     * 创建文章
     *
     * @author fengyexjtu@126.com
     * @date 2024/3/13 15:37
     */
    @PostMapping("/create")
    @Transactional
    public boolean createArticle(@RequestBody @Validated AuthorDTO authorDTO) {
        Author author = new Author();
        author.setName(authorDTO.getName());
        author.setBirthday(DateUtil.parse(authorDTO.getBirthday(), "YYYY-MM-dd"));
        log.info("author = {}", author);
        return authorService.save(author);
    }
    
    @PutMapping("/update/{id}")
    @Transactional
    public String updateArticle(@PathVariable(value = "id") Long id,@RequestParam(value = "name") String name) {
        Author author = new Author();
        author.setId(id);
        author.setName(name);
        boolean result = authorService.updateById(author);
        return result ? "修改成功": "修改失败";
    }
}
